package controllers

import (
	models "WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
)

func GetOrderDetails(c *gin.Context){
	err, info, total := services.GetAlldetails()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func DelOrderDetails(c *gin.Context){
	id := c.Query("orderDetailsId")

	fmt.Println(id)
	if id == ""{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.Deldetails(id)
	services.Delorders(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func UpdateOrderDetails(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	orderDetailsId := m["OrderDetailsId"]
	orderId := m["OrderId"]
	goodsId := m["GoodsId"]
	ordgoodsNumber := m["OrdGoodsNumber"]
	factoryId := m["FactoryId"]
	orderSumMoney := m["OrderSumMoney"]

	err := services.Updatedetails(models.Order_details{orderDetailsId,orderId,goodsId,ordgoodsNumber,factoryId,orderSumMoney,false})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func AddDetails(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	fmt.Println(m)
	orderDetailsId := m["Order_details_id"]
	orderId := m["Order_id"]
	goodsId := m["Goods_id"]
	ordgoodsNumber := m["Ordgoods_number"]
	factoryId := m["Factory_id"]
	orderSumMoney := m["Order_sum_money"]

	err := services.AddDetailsdata(models.Order_details{orderDetailsId,orderId,goodsId,ordgoodsNumber,factoryId,orderSumMoney,false})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}